//
//  ChildViewCell.m
//  Village
//
//  Created by 程虎 on 2017/9/7.
//  Copyright © 2017年 程虎. All rights reserved.
//

#import "ChildViewCell.h"
#import "PhotosContainerView.h"

@interface ChildViewCell()
@property (nonatomic, strong) PhotosContainerView *photosContainer;
@end

@implementation ChildViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *cellID = @"ChildViewCell";
    ChildViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    return cell;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    PhotosContainerView *photosContainer = [[PhotosContainerView alloc] initWithMaxItemsCount:9];
    _photosContainer = photosContainer;
    [self.contentView addSubview:photosContainer];
    
    CGFloat left = 15;
    CGFloat top = 10;
    LRViewRadius(self.icon, 20, 0.5, [UIColor clearColor]);
    
    self.icon.sd_layout
    .leftSpaceToView(self.contentView, left)
    .topSpaceToView(self.contentView, top)
    .widthIs(40)
    .heightIs(40);
    
    self.nameLab.sd_layout
    .leftSpaceToView(self.icon, 9.5)
    .topEqualToView(self.icon)
    .rightSpaceToView(self.contentView, left)
    .heightIs(20);
    
    self.shareBtn.sd_layout
    .rightSpaceToView(self.contentView, left)
    .centerYEqualToView(self.nameLab)
    .widthIs(30)
    .heightIs(30);
    
    self.timeLab.sd_layout
    .leftEqualToView(self.nameLab)
    .topSpaceToView(self.nameLab, 6.5)
    .rightEqualToView(self.nameLab)
    .heightIs(11);
    
    self.contentLab.sd_layout
    .leftEqualToView(self.icon)
    .topSpaceToView(self.icon, top)
    .rightEqualToView(self.shareBtn)
    .autoHeightRatio(0);
    
    _photosContainer.sd_layout
    .leftEqualToView(self.contentLab)
    .topSpaceToView(self.contentLab,top)
    .rightEqualToView(self.contentLab);
    
    self.newsTypeImg.sd_layout
    .leftEqualToView(self.contentLab)
    .widthIs(50)
    .heightIs(15);
    
    self.addressLab.sd_layout
    .leftSpaceToView(self.newsTypeImg, 7.5)
    .centerYEqualToView(self.newsTypeImg)
    .rightEqualToView(self.contentLab)
    .heightIs(11);
}

- (void)setModel:(VillageModel *)model
{
    _model = model;
    [self.icon sd_setImageWithURL:[NSURL URLWithString:[Tools resolutionHeadImage:_model.headimg]] placeholderImage:[UIImage imageNamed:@"user_icon"]];
    self.nameLab.text = _model.nickname.length > 0 ? _model.nickname : _model.publishuser;
    
    NSString *str = _model.publishtime;
    str = [str substringToIndex:19];//截取掉下标7之后的字符串
    self.timeLab.text = [NSString stringWithFormat:@"%@  %@阅读",str.length > 0 ? str: @"",_model.readnum];
    self.contentLab.text = _model.title;
    self.newsTypeImg.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld_headline",[[_model.code3 substringWithRange:NSMakeRange(3, 1)] integerValue] + 1]];
    self.addressLab.text = _model.aread;
    
    UIView *bottomView = self.contentLab;
//    LSLog(@"%@ headimg ------  %@\n ------ %@",_model.nickname,[Tools toJSONData:_model.headimg],[Tools leapImgAry:[Tools toJSONData:_model.headimg]]);
    _photosContainer.photoNamesArray = [Tools leapImgAry:[Tools toJSONData:_model.upimg]];
    
    if (_photosContainer.photoNamesArray.count > 0) {
        _photosContainer.hidden = NO;
        bottomView = _photosContainer;
    } else {
        _photosContainer.hidden = YES;
    }
    
    self.newsTypeImg.sd_layout.topSpaceToView(bottomView, 12.5);
    
    [self setupAutoHeightWithBottomView:self.newsTypeImg bottomMargin:10];
}

- (IBAction)shareBtnClick:(id)sender {
    if (self.shareBlock) {
        self.shareBlock(_model);
    }
}

@end
